﻿using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using appCode;

public partial class Pages_CommonPages_Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            
            GetTop2Competition();
            GetCompetitionTop();
            GetTop8Design();
            GetTop1Design();
            GetAwards();
        }

    }
    private void GetCompetitionTop() { 
            ManagerDA dao = new ManagerDA();
            Competition c = new Competition();
            c = dao.getCompetition();
            imgCompeTop.ImageUrl = "~/Images/" + c.Img;            
            DateTime da = new DateTime();    
            da= Convert.ToDateTime(c.StartDate);
            lblDate.Text = da.ToString("MM/dd/yyyy");
            lblName.Text = c.Name;
            txtDescription.Text = Filter(c.Description);            
            linkdetailCompetition.NavigateUrl = "DetailCompetion.aspx?PID=" + c.ComId;
    }

    private void GetTop2Competition()
    {
        ManagerDA dao = new ManagerDA();
        Competition c = new Competition();
        c = dao.getTop2Competition();
        imgCompeBottom.ImageUrl = "~/Images/" + c.Img;
        DateTime da = new DateTime();
        da = Convert.ToDateTime(c.StartDate);
        lblDateBottom.Text = da.ToString("MM/dd/yyyy");
        lblNameBottom.Text = c.Name;
        txtDescriptionBottom.Text = Filter(c.Description);
        linkDetailCompetitionBottom.NavigateUrl = "DetailCompetion.aspx?PID=" + c.ComId;
    }

    private void GetTop8Design()
    {        
        dtListDesign.DataSource = BDesign.GetTop8Design();
        dtListDesign.DataBind();
        
    }

    private void GetAwards()
    {
        dataListAwards.DataSource = Bcompetition.GetTopAwards();
        dataListAwards.DataBind();
        Competition c = new Competition();
        //c.ComI=Bcompetition.GetAllCompetition().Rows[0][""];
    }    
    private void GetTop1Design()
    {
        ManagerDA dao = new ManagerDA();
        Competition c = new Competition();
        c = dao.getTop2Competition();
        string nameDesign = c.Name;        
        Design d = new Design();
        d = dao.GetTop1Design (nameDesign);
        imgDesign.ImageUrl = "~/Images/" + c.Img;
        DateTime da = new DateTime();
        da = Convert.ToDateTime(d.PostDate);
        lblDesignDate.Text = da.ToString("MM/dd/yyyy");
        lblDesignName.Text = d.Name;
        txtDescriptionDesign.Text = Filter(d.Description);
        linkDesign.NavigateUrl = "DetailCompetion.aspx?PID=" + d.Id;
    }

    public static string Filter(string _txt)
    {
        try
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Insert(0, _txt);
            if (strBuilder.Length > 50)
            {
                return strBuilder.ToString().Substring(0, 120) + "...";
            }
            else
            {
                return strBuilder.ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    
}